import { Component, Input, OnInit } from '@angular/core';
import { HttpService } from '../../shared/service/http.service';
import { Router } from '@angular/router';
import { AppGlobalService } from '../../shared/service/app-global.service';

@Component({
    selector: 'app-icon-link',
    templateUrl: './icon-link.component.html',
    styleUrls: ['./icon-link.component.scss']
})
export class IconLinkComponent implements OnInit {

    @Input() tagData: any;

    types: any[];
    aWidth: string;
    targetObj: any;

    constructor(
        private http: HttpService,
        private router: Router,
        private appGlobalService: AppGlobalService
    ) {
    }

    ngOnInit() {
        this.types = [];
        this.dealData(this.tagData);
    }

    jumpToPage(item) {
        const sessionId = localStorage.getItem('sessionId');
        const pageParams = {
            pageId: item.target,
            params: {},
            sessionId: sessionId
        };
        this.appGlobalService.goToForward(pageParams);
    }

    dealData(data) {
        if (data.limit && data.limit > 4) {
            this.aWidth = 100 / data.limit * 2 + '%';
        } else {
            data.limit = 4;
            this.aWidth = 100 / data.limit + '%';
        }

        let num = 0, index = 0;
        this.types[index] = [];
        data.links.forEach((v) => {
            if (num < data.limit) {
                this.types[index].push(v);
                ++num;
            } else {
                num = 0;
                ++index;
                this.types[index] = [];
                this.types[index].push(v);
            }
        });
    }

    /*
        slideChanged(val: any) {
            const slideIndex = val.getActiveIndex();
            this.slideActive = slideIndex % 2 !== 0;
        }*/
}
